<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>正则表达式之入门</title>

	<style type="text/css">
		* {
			padding: 0;
			margin: 0;
			box-sizing: border-box;
		}
		.test {
			width: 600px;
			height: 400px;
			margin: 100px auto
		}
		.test div {
			padding: 10px;
			background-color: #12B4DB;
		}
		.test .test-title {
			display: inline-block;
			font-size: 18px;
			color: #000;
			width: 250px;
			text-align: right;
		}
	</style>
</head>
<body>
<div class="test">
	<div class="test-tel">
		<label class="test-title">测试是否为手机号码：</label>
		<input id="tel" type="number" name="">
		<label id="tel-result">否</label>
	</div>
	<div class="test-word">
		<label class="test-title">测试是否有相邻重复单词：</label>
		<input id="word" type="text" name="">
		<label id="word-result">否</label>
	</div>
</div>
<script type="text/javascript">
	var tel = document.getElementById("tel");
	tel.addEventListener("change",testTel,false);
	var word = document.getElementById("word");
	word.addEventListener("change",testWord,false);
	function testTel(){
		var reg = new RegExp("1([378][0-9]|5[012356789]|4[57])[0-9]{8}\\b");
		var telnumber = tel.value;
		var result = reg.test(telnumber);
		if(result){
			document.getElementById("tel-result").innerText="是";
		}else{
			document.getElementById("tel-result").innerText="否";
		}
	}
	function testWord(){
		var reg = new RegExp("\\b([A-Za-z]+)\\b\\s+\\b\\1\\b");
		var wordvalue = word.value;
		var result = reg.test(wordvalue);
		if(result){
			document.getElementById("word-result").innerText="是";
		}else{
			document.getElementById("word-result").innerText="否";
		}
	}
</script>
</body>
</html>